package com.general.framework.zzyy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

public class LongAdderDemo {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个LongAdder对象用于计数
        LongAdder longAdder = new LongAdder();
        // 创建一个线程池，这里使用固定大小为10的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 提交100个任务到线程池，每个任务对LongAdder进行自增操作
        for (int i = 0; i < 100; i++) {
            executorService.submit(() -> {
                longAdder.add(1);
            });
        }
        // 关闭线程池，等待所有任务完成
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
        // 输出最终的计数结果
        System.out.println("计数结果: " + longAdder.sum());
    }
}